﻿namespace Arms.Application.Modules.People.Customers
{
    using Arms.Application.Modules.Common.DTOs;
    using Arms.Application.Modules.People.Customers.DTOs;
    using Arms.Application.Modules.People.DTOs;
    using Arms.Application.Services;
    using Arms.Domain;
    using Arms.Domain.Modules.Orders;
    using Arms.Domain.Modules.People;
    using System;
    using System.Collections.Generic;
    using System.Linq;

    public class CustomerQueryService : ServiceBase, ICustomerQueryService
    {
        public CustomerQueryService(IUnitOfWork uow) : base(uow) { }

        public bool CustomerExists(string name, string phoneNumber)
        {
            return base.UnitOfWork.Repository<Customer>().GetAll(x => x.Person.Contact.FirstName == name &&
                                                                      (x.Person.Contact.PhoneNumber == phoneNumber ||
                                                                       x.Person.Contact.CellNumber == phoneNumber ||
                                                                       x.Person.Contact.FaxNumber == phoneNumber))
                                                         .Count() > 0;
        }

        public bool CustomerExists(ContactDTO contact)
        {
            return base.UnitOfWork.Repository<Customer>().GetAll(x => x.Person.Contact.FirstName == contact.FirstName &&
                                                                      x.Person.Contact.LastName == contact.LastName &&
                                                                      x.Person.Contact.EmailAddress == contact.EmailAddress &&
                                                                      (x.Person.Contact.PhoneNumber == contact.PhoneNumber ||
                                                                       x.Person.Contact.CellNumber == contact.CellNumber ||
                                                                       x.Person.Contact.FaxNumber == contact.FaxNumber))
                                                         .Count() > 0;
        }

        public IEnumerable<CustomerDTO> GetCustomers()
        {
            return base.UnitOfWork.Repository<Customer>().GetAll().Select(x => new CustomerDTO()
            {
                CustomerID = x.ID,
                Person = new PersonDTO()
                {
                    Contact = new ContactDTO()
                    {
                        ContactID = x.Person.Contact.ID,
                        Title = x.Person.Contact.Title,
                        FirstName = x.Person.Contact.FirstName,
                        MiddleName = x.Person.Contact.MiddleName,
                        LastName = x.Person.Contact.LastName,
                        PhoneNumber = x.Person.Contact.PhoneNumber,
                        CellNumber = x.Person.Contact.CellNumber,
                        FaxNumber = x.Person.Contact.FaxNumber,
                        EmailAddress = x.Person.Contact.EmailAddress
                    }
                }
            });
        }


        public CustomerDTO FindCustomer(int orderHeaderID)
        {
            var orderHeader = base.UnitOfWork.Repository<OrderHeader>().Find(orderHeaderID);
            return new CustomerDTO()
            {
                CustomerID = orderHeader.Customer.ID,
                Person = new PersonDTO()
                {
                    Contact = new ContactDTO()
                    {
                        ContactID = orderHeader.Customer.Person.Contact.ID,
                        Title = orderHeader.Customer.Person.Contact.Title,
                        FirstName = orderHeader.Customer.Person.Contact.FirstName,
                        MiddleName = orderHeader.Customer.Person.Contact.MiddleName,
                        LastName = orderHeader.Customer.Person.Contact.LastName,
                        PhoneNumber = orderHeader.Customer.Person.Contact.PhoneNumber,
                        CellNumber = orderHeader.Customer.Person.Contact.CellNumber,
                        FaxNumber = orderHeader.Customer.Person.Contact.FaxNumber,
                        EmailAddress = orderHeader.Customer.Person.Contact.EmailAddress
                    }
                }
            };
        }


        public int FindCustomerID(string name, string phoneNumber)
        {
            var customer = base.UnitOfWork.Repository<Customer>().Single(x => x.Person.Contact.FirstName == name &&
                                                                              (x.Person.Contact.PhoneNumber == phoneNumber ||
                                                                               x.Person.Contact.CellNumber == phoneNumber ||
                                                                               x.Person.Contact.FaxNumber == phoneNumber));

            if (customer != null)
            {
                return customer.ID;
            }
            return 0;
        }
    }
}
